﻿using System;
using System.Collections.Generic;
using IQIGame.Onigao.GamePlay;
using LFloatMath.Math;

namespace IQIGame.Onigao.Logic
{
    public class FormulaFunction_If : FormulaFunction
    {
        public override EFormulaFunctionType funcType => EFormulaFunctionType.If;

        public override void Calculate(Stack<IFormulaOperand> rCalopd, IFormulaObject rSelf, IFormulaObject rTarget, Formula rFromFormula)
        {
            IFormulaOperand opd1 = rCalopd.Pop();
            IFormulaOperand opd2 = rCalopd.Pop();
            IFormulaOperand opd3 = rCalopd.Pop();
            if (opd3.TryGet<bool>(out var op3, rSelf, rTarget, rFromFormula))
            {
                rCalopd.Push(op3 ? opd2 : opd1);
            }
            else
            {
                throw new Exception("If func need one bool And two other operand");
            }

            opd3.TryFree();
            if (op3)
            {
                opd2.TryFree();
            }
            else
            {
                opd1.TryFree();
            }
        }
    }
}